package gretha.modelsDeJTable;

import gretha.donnees.Massif;

import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;

public class MassifTableModel extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1282480207120524423L;
	private Massif[] donnees;
	private ArrayList<Massif> tblMassifs;
	private String[] enTetes = { "Nom", "Suppression" };

	public MassifTableModel(Massif[] data, ArrayList<Massif> tblMassifs) {
		super();
		donnees = data;
		this.tblMassifs = new ArrayList<Massif>();
		this.tblMassifs = tblMassifs;
	}

	@Override
	public int getColumnCount() {
		return enTetes.length;
	}

	@Override
	public int getRowCount() {
		return donnees.length;
	}

	public String getColumnName(int columnIndex) {
		return enTetes[columnIndex];
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return new String(donnees[rowIndex].getNom());
		case 1:
			return new JButton("Supprimer");
		}
		return "";
	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
		boolean editable = false;
		if (columnIndex == 1) {
			editable = true;
		}
		return editable;

	}

	public Massif[] getDonnees() {
		return donnees;
	}

	public void setDonnees(Massif[] donnees) {
		this.donnees = donnees;
	}

	public void supprimerLigne(int selectedLine) {
		Massif tmp = donnees[selectedLine];
		tblMassifs.remove(tmp);

		Massif[] temp = new Massif[donnees.length - 1];
		for (int i = 0; i < selectedLine; i++) {
			temp[i] = donnees[i];
		}

		for (int i = selectedLine; i < donnees.length - 1; i++) {
			temp[i] = donnees[i + 1];
		}
		donnees = new Massif[temp.length];
		donnees = temp;
	}

	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
	}

	public Class<? extends Object> getColumnClass(int columnIndex) {
		switch (columnIndex) {
		case 0:
			return String.class;
		case 1:
			return JButton.class;
		default:
			return Object.class;
		}
	}
}
